class Crd2pulumi < Formula
  desc "Generate typed CustomResources from a Kubernetes CustomResourceDefinition"
  homepage "https://github.com/pulumi/crd2pulumi"
  url "https://github.com/pulumi/crd2pulumi/archive/refs/tags/v1.6.0.tar.gz"
  sha256 "65ba960325bfbd288b1cbe4675e1c24fd2fe6422b2cffc46a74fa801063beba6"
  license "Apache-2.0"
  head "https://github.com/pulumi/crd2pulumi.git", branch: "master"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_tahoe:   "c380a1dbe25ab6bc39fd1a6fb7f4d676ffbdfdd1ac6f5fe6de532f16e95663a8"
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "2ab981239750c76bf83cb6d29fd9c0486596c22a125318e760bb9b4877ca050b"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "037495a268751f289edfd7c1ef440dcb609ec7e21c139c26d78279fe7613daf5"
    sha256 cellar: :any_skip_relocation, sonoma:        "ef183fcd4992a46265574b8946c96993beb1cdf93a9d15a928b4970ca6f8eaef"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "7d7228a160a405a73fdd32f62e83b3cbae37e5f2f5459e079ce184c96d0432e4"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "20318e7be374b5f94eb7ad06a0cd7580ca2d1648cfeeb619f6d835824af47f6c"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w -X github.com/pulumi/crd2pulumi/cmd.Version=#{version}")

    generate_completions_from_executable(bin/"crd2pulumi", "completion")
  end

  test do
    assert_match version.to_s, shell_output("#{bin}/crd2pulumi version")

    test_crd = testpath/"crd.yaml"
    test_crd.write <<~YAML
      apiVersion: apiextensions.k8s.io/v1
      kind: CustomResourceDefinition
      metadata:
        name: widgets.example.com
      spec:
        group: example.com
        names:
          kind: Widget
          plural: widgets
          singular: widget
        scope: Namespaced
        versions:
          - name: v1
            served: true
            storage: true
            schema:
              openAPIV3Schema:
                type: object
                properties:
                  spec:
                    type: object
                    properties:
                      size:
                        type: string
                      color:
                        type: string
    YAML

    # Generate TypeScript code from the CRD
    system bin/"crd2pulumi", "--nodejsPath", testpath/"typescript", test_crd, "--force"

    assert_path_exists testpath/"typescript/example/v1/widget.ts"
    assert_match "this file was generated by crd2pulumi", (testpath/"typescript/provider.ts").read

    # Generate Python code from the CRD
    system bin/"crd2pulumi", "--pythonPath", testpath/"python", "--python", test_crd
    assert_path_exists testpath/"python/pulumi_crds/example/v1/Widget.py"
    assert_match "this file was generated by crd2pulumi", (testpath/"python/pulumi_crds/provider.py").read
  end
end
